package com.tencent.mars.xlog;


import android.os.Environment;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;

public class LogUtil {

    private static LogUtil instance;

    private String logPath;

    private boolean openLog;

    /**
     * 是否初始化
     */
    private boolean init = false;

    private LogUtil() {
    }

    public static synchronized LogUtil getInstance() {
        if (instance == null) {
            instance = new LogUtil();
        }
        return instance;
    }

    /**
     * 初始化XLog
     *
     */
    public void init(boolean openLog) {
        this.openLog = openLog;
        if (!init && openLog){
            String sdCard = Environment.getExternalStorageDirectory().getAbsolutePath();
            this.logPath = sdCard + "/testLog";;
            System.loadLibrary("c++_shared");
            System.loadLibrary("marsxlog");

            Xlog.appenderOpen(Xlog.LEVEL_VERBOSE, Xlog.AppednerModeAsync, "", logPath, "testLog");
            Xlog.setConsoleLogOpen(true);

            Log.setLogImp(new Xlog());
            init = true;
        }
    }

    /**
     * 销毁
     */
    public void close() {
        deleteFile(new File(this.logPath));
        Log.appenderClose();

    }

    /**
     * 删除日志文件
     *
     * @param file
     */
    private void deleteFile(File file) {
        if (file.exists()) {
            if (file.isFile()) {
                boolean result = file.delete();
                if (result){
                    //do something just for sonar
                }
            } else if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    this.deleteFile(files[i]);
                }
                boolean result = file.delete();
                if (result){
                    //do something just for sonar
                }
            }
        }
    }

    public void saveAndClose() {
        if (init && openLog){
            Log.appenderClose();
        }
    }

    /**
     * 打印日志
     *
     * @param tag
     * @param msg
     */
    public void log(String tag, String msg) {
        if (init && openLog) {
            //添加打印信息，过长的问题
            while (msg.length() >= 2000) {
                Log.v(tag, msg.substring(0, 2000));
                msg = msg.substring(2000);
            }

            Log.v(tag, msg);
        }
    }

    /**
     * 打印异常信息
     *
     * @param tag
     * @param msg
     * @param throwable
     */
    public void logException(String tag, String msg, Throwable throwable) {
        if (init && openLog) {
            Log.d(tag, msg + "/\n" + getErrorInfo(throwable));
        }
    }

    /**
     * 获取详细信息
     *
     * @param arg1
     * @return
     */
    private String getErrorInfo(Throwable arg1) {
        Writer writer = new StringWriter();
        PrintWriter pw = new PrintWriter(writer);
        arg1.printStackTrace(pw);
        pw.close();
        String error = writer.toString();
        return error;
    }

    public void setOpenLog(boolean openLog) {
        this.openLog = openLog;
    }
}
